/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * asignarCita.java
 *
 * Created on 21/03/2012, 03:48:24 PM
 */
package com.torresquintero.oncologos.secretaria.vista;

import com.torresquintero.oncologos.common.entityUtilities.MedicoUtilities;
import com.torresquintero.oncologos.common.factory.FactoryData;
import com.torresquintero.oncologos.common.factory.Mensajes;
import com.torresquintero.oncologos.common.vista.PanelOncologos;
import com.torresquintero.oncologos.common.vista.VentanaPrincipalManager;
import com.torresquintero.oncologos.entidades.Medico;
import com.torresquintero.oncologos.entidades.Paciente;
import com.torresquintero.oncologos.enumeraciones.MotivoConsultaEnum;
import com.torresquintero.oncologos.enumeraciones.TipoDocumentoEnum;
import com.torresquintero.oncologos.medico.vista.PanelAgenda;
import com.torresquintero.oncologos.secretaria.controladores.AsignarConsultasController;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author diana
 */
public class PanelAsignarCita extends PanelOncologos {

    private AsignarConsultasController control;

    /** Creates new form asignarCita */
    public PanelAsignarCita() {
        initComponents();
        setTitulo(Mensajes.getMensaje("PanelAgenda.titulo"));
        control = AsignarConsultasController.getInstance();

        TipoDocumentoEnum tipoDocs[] = FactoryData.getTipoDocumentos();
        llenarComboBoxes();
    }

    /**
     * Metodo para llenar los comboboxes de la ventana.
     */
    public void llenarComboBoxes() {
        TipoDocumentoEnum tipoDocs[] = FactoryData.getTipoDocumentos();
        for (TipoDocumentoEnum tipoDocumentoEnum : tipoDocs) {
            jcbTipoDoc.addItem(tipoDocumentoEnum);
        }

        MotivoConsultaEnum motivos[] = FactoryData.getMotivosConsultaEnum();
        for (MotivoConsultaEnum motivoConsultaEnum : motivos) {
            jCBMotivo.addItem(motivoConsultaEnum);
        }

        List<Medico> medicos = MedicoUtilities.listarTodosLosMedicos();
        for (Medico medico : medicos) {
            jCBMedico.addItem(medico);
        }

        for (int i = 0; i < 23; i++) {
            jCBHora.addItem(i);
           
        }
        
         for (int i = 0; i < 59; i++) {
            jCBMin.addItem(i);
           
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jcbTipoDoc = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jTxDocumento = new javax.swing.JTextField();
        botonOncologos1 = new com.torresquintero.oncologos.common.vista.BotonOncologos();
        jBBuscar = new com.torresquintero.oncologos.common.vista.BotonOncologos();
        jLabel7 = new javax.swing.JLabel();
        tfNOmbre = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        tfEps = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jCBHora = new javax.swing.JComboBox();
        jBAsignar = new com.torresquintero.oncologos.common.vista.BotonOncologos();
        jLabel5 = new javax.swing.JLabel();
        jCBMotivo = new javax.swing.JComboBox();
        jCBMin = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jCBMedico = new javax.swing.JComboBox();
        jBAgenda = new com.torresquintero.oncologos.common.vista.BotonOncologos();
        jLabel4 = new javax.swing.JLabel();
        dcFechaCita = new com.toedter.calendar.JDateChooser();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos Paciente", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        jLabel1.setText("Tipo Documento:");

        jLabel2.setText("Documento:");

        botonOncologos1.setText("botonOncologos1");

        jBBuscar.setText("Buscar");
        jBBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBBuscarActionPerformed(evt);
            }
        });

        jLabel7.setText("Nombre");

        tfNOmbre.setEditable(false);

        jLabel8.setText("EPS");

        tfEps.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(botonOncologos1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tfEps, javax.swing.GroupLayout.DEFAULT_SIZE, 441, Short.MAX_VALUE)
                    .addComponent(tfNOmbre, javax.swing.GroupLayout.DEFAULT_SIZE, 441, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jcbTipoDoc, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTxDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                        .addComponent(jBBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jcbTipoDoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTxDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 126, Short.MAX_VALUE)
                        .addComponent(botonOncologos1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfNOmbre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfEps, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addGap(74, 74, 74))))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Cita", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jBAsignar.setText("Asignar");
        jBAsignar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAsignarActionPerformed(evt);
            }
        });

        jLabel5.setText("Motivo");

        jLabel3.setText("Fecha:");

        jLabel6.setText("Medico:");

        jBAgenda.setText("Ver Agenda");
        jBAgenda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAgendaActionPerformed(evt);
            }
        });

        jLabel4.setText("Hora:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(dcFechaCita, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(jCBMotivo, 0, 164, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jBAsignar, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                            .addComponent(jCBMedico, 0, 167, Short.MAX_VALUE))))
                .addGap(57, 57, 57)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jCBHora, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCBMin, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jBAgenda, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(161, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(jLabel4)
                        .addComponent(jCBHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jCBMin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(dcFechaCita, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jCBMotivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jCBMedico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBAgenda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jBAsignar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jBBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBBuscarActionPerformed
        // TODO add your handling code here:
        Paciente paciente = control.buscarPaciente(jTxDocumento.getText(), 
                (TipoDocumentoEnum) jcbTipoDoc.getSelectedItem());
        
        if(paciente!=null){
            tfNOmbre.setText(paciente.getPrimerNombre()+" "+paciente.getPrimerApellido());
            tfEps.setText(paciente.getEps().toString());
        }else{
            int opc=JOptionPane.showConfirmDialog(null,"No esta registrado en el sistema de informacion, desea registrarlo?");
            
            if(opc==JOptionPane.YES_OPTION){
                VentanaPrincipalManager.agregarVentana(new PanelCrearPaciente());
                //TODO: agregar ventaa crear paciente.
            }
        }
    }//GEN-LAST:event_jBBuscarActionPerformed

    private void jBAgendaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAgendaActionPerformed
        // TODO add your handling code here:

        VentanaPrincipalManager.agregarVentana(new PanelAgenda());
    }//GEN-LAST:event_jBAgendaActionPerformed

    private void jBAsignarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAsignarActionPerformed
        
        try {
            // TODO add your handling code here:
            control.setMedico((Medico)jCBMedico.getSelectedItem());
            Date fecha=dcFechaCita.getDate();
            fecha.setHours((Integer)jCBHora.getSelectedItem());
            fecha.setMinutes((Integer)jCBMin.getSelectedItem());
            control.setFechaConsulta(fecha);
            control.setMotivo((MotivoConsultaEnum)jCBMotivo.getSelectedItem());
            
            control.asignarCita();
            JOptionPane.showMessageDialog(null, "Cita Asignada con exito","Error",JOptionPane.ERROR_MESSAGE);

        } catch (Exception ex) {
            Logger.getLogger(PanelAsignarCita.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
        }
        
        
        
    }//GEN-LAST:event_jBAsignarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.torresquintero.oncologos.common.vista.BotonOncologos botonOncologos1;
    private com.toedter.calendar.JDateChooser dcFechaCita;
    private com.torresquintero.oncologos.common.vista.BotonOncologos jBAgenda;
    private com.torresquintero.oncologos.common.vista.BotonOncologos jBAsignar;
    private com.torresquintero.oncologos.common.vista.BotonOncologos jBBuscar;
    private javax.swing.JComboBox jCBHora;
    private javax.swing.JComboBox jCBMedico;
    private javax.swing.JComboBox jCBMin;
    private javax.swing.JComboBox jCBMotivo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTxDocumento;
    private javax.swing.JComboBox jcbTipoDoc;
    private javax.swing.JTextField tfEps;
    private javax.swing.JTextField tfNOmbre;
    // End of variables declaration//GEN-END:variables
}
